package com.jt.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.util.UserThreadLocal;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    private final static String JTUSER = "JT_USER";
    //添加接口
    @Reference(check = false)
    private DubboCartService cartService;
    @Reference(check = false)
    private DubboOrderService orderService;

    @RequestMapping("create")
    public String create(Model model){

        Long userId = UserThreadLocal.get().getId();
        List<Cart> cartList = cartService.findCartList(userId);
        model.addAttribute("carts",cartList);
        return "order-cart";
    }
    /**
     * 实现订单入库
     * url:http://www.jt.com/order/submit
     * 参数: 整个form表单    利用order对象接收
     * 返回值: SysResult对象    返回orderId
     * 业务: 订单入库时应该入库3张表记录. order  orderShipping  orderItems
     *       orderId由登录用户id+当前时间戳手动 拼接.
     *       并且要求三个对象的主键值相同.
     */
    @RequestMapping("/submit")
    @ResponseBody
    public SysResult submit(Order order){
        Long userId = UserThreadLocal.get().getId();
        order.setUserId(userId);
        //完成订单入库 并且返回orderId
        String orderId = orderService.saveOrder(order);
        return SysResult.success(orderId);
    }
    /**
     * 实现订单的查询 根据orderId
     * url地址: http://www.jt.com/order/success.html?id=71598258019985
     * 参数: id=71598258019985
     * 返回值: success页面
     * 页面参数: ${order.orderId}
     */
    @RequestMapping("/success")
    public String findOrderById(String id,Model model){
        Order order = orderService.findOrderById(id);
        model.addAttribute("order",order);
        return "success";
    }
}
